package com.yesep.learn.hik.sdk;

import javax.websocket.Session;

import com.sun.jna.NativeLong;
import com.sun.jna.Pointer;
import com.sun.jna.ptr.ByteByReference;
import com.sun.jna.ptr.NativeLongByReference;

/**
 * 预览回调
 */
public class FRealDataCallBack implements HCNetSDK.FRealDataCallBack_V30 {
    private DecCBack dec;
    private boolean loading = false;
    private NativeLongByReference m_lPort = new NativeLongByReference(new NativeLong(-1));

    public FRealDataCallBack(Session session) {
        dec = new DecCBack(session);
    }

    @Override
    public void invoke(NativeLong lRealHandle, int dwDataType, ByteByReference pBuffer, int dwBufSize, Pointer pUser) {

        /**
         * 让 loading 一直保持 为true
         * 说明 还有码流数据 在回传
         */
        loading = true;

        switch (dwDataType) {
            case HCNetSDK.NET_DVR_SYSHEAD: //系统头
                if (!SDKUtils.getPlayControl().PlayM4_GetPort(m_lPort)) {//获取播放库未使用的通道号
                    break;
                }

                if (dwBufSize > 0) {
                    //设置实时流播放模式
                    if (!SDKUtils.getPlayControl().PlayM4_SetStreamOpenMode(m_lPort.getValue(), PlayCtrl.STREAME_REALTIME)) {
                        break;
                    }
                    //打开流接口
                    if (!SDKUtils.getPlayControl().PlayM4_OpenStream(m_lPort.getValue(), pBuffer, dwBufSize, 1024 * 1024)) {
                        break;
                    }
                    //设置解码器
                    if (!SDKUtils.getPlayControl().PlayM4_SetDecCallBack(m_lPort.getValue(), dec)) {
                        break;
                    }
                    //播放开始
                    if (!SDKUtils.getPlayControl().PlayM4_Play(m_lPort.getValue(), null)) {
                        break;
                    }
                }
            case HCNetSDK.NET_DVR_STREAMDATA: //视频流数据
                if (dwBufSize > 0) {
                    try {
                        if (m_lPort.getValue().intValue() != -1 && dec != null && dec.getSession() != null && dec.getSession().isOpen()) {
                            try {
                                if (!SDKUtils.getPlayControl().PlayM4_InputData(m_lPort.getValue(), pBuffer, dwBufSize)) {
                                    break;
                                }
                            } catch (Exception e) {
                                loading = false;
                                e.printStackTrace();
                                dec.getSession().close();
                                System.out.println("关闭连接");
                                return;
                            }
                        }
                    } catch (Exception e) {
                        loading = false;
                        e.printStackTrace();
                    }
                }
                break;

            case HCNetSDK.NET_DVR_AUDIOSTREAMDATA: //音频流数据

                break;

            case HCNetSDK.NET_DVR_REALPLAYEXCEPTION: //预览异常
                loading = false;
                break;
            case HCNetSDK.NET_DVR_REALPLAYNETCLOSE: //预览时连接断开
                loading = false;
                break;
            case HCNetSDK.NET_DVR_REALPLAY5SNODATA: //预览5s没有收到数据
                loading = false;
                break;
            case HCNetSDK.NET_DVR_PLAYBACKEXCEPTION: //回放异常
                loading = false;
                break;
            case HCNetSDK.NET_DVR_PLAYBACKNETCLOSE: //回放时候连接断开
                loading = false;
                break;
            case HCNetSDK.NET_DVR_PLAYBACK5SNODATA: //回放5s没有收到数据
                loading = false;
                break;
        }
    }

    public boolean isLoading() {
        return loading;
    }

    public void setLoading(boolean loading) {
        this.loading = loading;
    }
}